###########################################################
# Advance version contron information generator
###########################################################

VERSION_PART0 = 1
VERSION_PART1 = 0
VERSION_PART2 = 0
VERSION_PART3 = 0

VERSION_GIT   = 0
VERSION_SVN   = 0

win32 {
    VERSION_BUILD_PTS       = $$system(echo "%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%")
    contains(QMAKE_COMPILER, 'msvc') {
        VERSION_COMPILER    = MSVC-$${QMAKE_MSC_FULL_VER}
    } else {
        VERSION_COMPILER    = MinGW-$${QMAKE_GCC_MAJOR_VERSION}.$${QMAKE_GCC_MINOR_VERSION}.$${QMAKE_GCC_PATCH_VERSION}
    }

    # VERSION_SVN_ALL         = $$system(svn info $${CYG_CPP_ROOT} -r HEAD)
    VERSION_SVN_ALL         = $$system(svn info $${CYG_CPP_ROOT})
    VERSION_SVN_ALL         = $$val_escape(VERSION_SVN_ALL)

    !isEmpty(VERSION_SVN_ALL) {
        VERSION_SVN_REV     = $$quote($$system(svn info $${CYG_CPP_ROOT} | findstr "Rev:"))
        VERSION_SVN_REV     = $$section(VERSION_SVN_REV, " ", 3, 3)
        message($$VERSION_SVN_REV)
    }
}

unix {
    VERSION_BUILD_PTS       = $$system(date '+%Y%m%d%H%M%S')
    VERSION_COMPILER        = GNU-$${QMAKE_GCC_MAJOR_VERSION}.$${QMAKE_GCC_MINOR_VERSION}.$${QMAKE_GCC_PATCH_VERSION}

    VERSION_SVN_ALL         = $$system(svn info $${CYG_CPP_ROOT})
    !isEmpty(VERSION_SVN_ALL) {
        VERSION_SVN_REV     = $$system("svn info $${CYG_CPP_ROOT} | tail -n 3 | awk 'NR == 1 {print $4}'")
    }
}

isEmpty(VERSION_SVN_ALL) {
    VERSION_GIT_SHA1        = $$system(git log --format=%h -1)
    !isEmpty(VERSION_GIT_SHA1) {
        VERSION_GIT_DESC    = $$system(git describe --tags)
        VERSION_PART0       = $$section(VERSION_GIT_DESC, ".", 0, 0)
        VERSION_PART0       = $$replace(VERSION_PART0, 'v', '')
        VERSION_PART1       = $$section(VERSION_GIT_DESC, ".", 1, 1)
        VERSION_PART2       = $$section(VERSION_GIT_DESC, ".", 2, 2)
        VERSION_PART3       = $$section(VERSION_GIT_DESC, ".", 3, 3)
        VERSION_PARTN       = $$section(VERSION_PART3, "-", 0, 0)
        !isEmpty(VERSION_PARTN) {
            VERSION_PART3   = $$VERSION_PARTN
        }

        VERSION_GIT_TAG     = $$section(VERSION_GIT_DESC, "-", 0, 1)
        isEmpty(VERSION_GIT_TAG) {
            VERSION_GIT_TAG = $$VERSION_GIT_DESC
        }
        message($$VERSION_GIT_TAG $$VERSION_GIT_SHA1)

        VERSION_GIT         = 1
    }
} else {
    VERSION_SVN             = 1
}

CYG_VERSION_INFO = \
                   "$${LITERAL_HASH}ifndef __VERSION_H__" \
                   "$${LITERAL_HASH}define __VERSION_H__" \
                   "$${LITERAL_HASH}define VERSION_COPYRIGHT       \"Contron, Ltd. <caiwang213@qq.com>\"" \
                   "$${LITERAL_HASH}define VERSION_BUILD_PTS       \"$$VERSION_BUILD_PTS\"" \
                   " " \
                   "$${LITERAL_HASH}define VERSION_GIT             $$VERSION_GIT" \
                   "$${LITERAL_HASH}define VERSION_GIT_TAG         \"$$VERSION_GIT_TAG\"" \
                   "$${LITERAL_HASH}define VERSION_GIT_SHA1        \"$$VERSION_GIT_SHA1\"" \
                   " " \
                   "$${LITERAL_HASH}define VERSION_SVN             $$VERSION_SVN" \
                   "$${LITERAL_HASH}define VERSION_SVN_ALL         \"$$VERSION_SVN_ALL\"" \
                   "$${LITERAL_HASH}define VERSION_SVN_TAG         \"$${VERSION_PART0}.$${VERSION_PART1}.$${VERSION_PART2}.$${VERSION_PART3}\"" \
                   "$${LITERAL_HASH}define VERSION_SVN_REV         \"r$$VERSION_SVN_REV\"" \
                   " " \
                   "$${LITERAL_HASH}if VERSION_GIT" \
                   "  $${LITERAL_HASH}define VERSION_TAG           VERSION_GIT_TAG" \
                   "  $${LITERAL_HASH}define VERSION_REV           VERSION_GIT_SHA1" \
                   "$${LITERAL_HASH}else" \
                   "  $${LITERAL_HASH}define VERSION_TAG           VERSION_SVN_TAG" \
                   "  $${LITERAL_HASH}define VERSION_REV           VERSION_SVN_REV" \
                   "$${LITERAL_HASH}endif" \
                   " " \
                   "$${LITERAL_HASH}define VERSION_PART0           $$VERSION_PART0" \
                   "$${LITERAL_HASH}define VERSION_PART1           $$VERSION_PART1" \
                   "$${LITERAL_HASH}define VERSION_PART2           $$VERSION_PART2" \
                   "$${LITERAL_HASH}define VERSION_PART3           $$VERSION_PART3" \
                   " " \
                   "$${LITERAL_HASH}define VERSION_COMPILER        \"$$VERSION_COMPILER\"" \
                   " " \
                   "$${LITERAL_HASH}endif" 

write_file("$${CYG_CPP_ROOT}/version.h", CYG_VERSION_INFO)
